
import { removeLayer, getTileLayers, createTileLayer } from './layerHandle'
import OlMarks from '../mark'
import OlMapPicker from '../mapPicker'
import OlLocationMark from '../mapPicker/Location.js'
import OlDrawShap from '../drawShap'
import OlMapTrajectory from '../trajectory'
import OlMapTrajectorys from '../trajectorys'
/**
 * 创建地图影像
 * @param {*} layerCore
 * @param {*} config
 */
let newMarks, newPickerPoint, newLocatPoint, newDrawShap, newTrajectory,newTrajectorys
export function createLayer (map, config) {
  if (config.type === 'TileLayer') { // 基础图层
    createTileLayer(map, config.layerProperties)
  } else if (config.type === 'MarkerLayer') { // markers 图层
    newMarks = new OlMarks(map, config)
  } else if (config.type === 'MapPickerLayer') { // 地图拾取器图层
    newPickerPoint = new OlMapPicker(map, config)
  } else if (config.type === 'LocationLayer') { //  定位图层
    newLocatPoint = new OlLocationMark(map, config)
  } else if (config.type === 'DrawShapLayer') { // 绘制图层
    newDrawShap = new OlDrawShap(map, config)
  } else if (config.type === 'TrajectoryLayer') { // 轨迹图层
    newTrajectory = new OlMapTrajectory(map, config)
  } else if (config.type === 'TrajectoryLayers') { // 多条轨迹图层（新版本）
    newTrajectorys = new OlMapTrajectorys(map, config)
  }
}
/**
 * 更新layers
 * @param {*} map
 * @param {*} config
 */
export function updateLayers (map, config) {
  if (config.type === 'MarkerLayer') { // markers 图层
 
    if (newMarks) {
      newMarks.updateOlMars(map, config)
    } else {
      newMarks = new OlMarks(map, config)
    }
  } else if (config.type === 'MapPickerLayer') { // 地图拾取器图层
    if (newPickerPoint) {
      newPickerPoint.updateOlMapPicker(map, config)
    } else {
      newMarks = new OlMapPicker(map, config)
    }
  } else if (config.type === 'LocationLayer') { //  定位图层
    if (newLocatPoint) {
      newLocatPoint.updateOlLocationMark(map, config)
    } else {
      newLocatPoint = new OlLocationMark(map, config)
    }
  } else if (config.type === 'DrawShapLayer') { // 绘制图层

    if (newDrawShap) {
      newDrawShap.updateOlDrawShap(map, config)
    } else {
      
      newDrawShap = new OlDrawShap(map, config)
    }
  } else if (config.type === 'TrajectoryLayer') { // 轨迹图层
    if (newTrajectory) {
      newTrajectory.updateTrajectory(map, config)
    } else {
      newTrajectory = new OlMapTrajectory(map, config)
    }
  } else if (config.type === 'TrajectoryLayers') { // 轨迹图层
    if (newTrajectorys) {
      newTrajectorys.updateTrajectory(map, config)
    } else {
      newTrajectorys = new OlMapTrajectorys(map, config)
    }
  }
}

/**
 * 判断是否已经存在layer,防止覆盖
 */

/**
 * 获取当前配置的layer
 * @param {*} configs
 * @returns
 */
export function getConfigLayers (configs) {
  // console.log('获取当前配置Layer', configs)
  let layerArrs = []
  for (const config in configs) {
    // console.log('config', config, config.source)
    const item = configs[config]
    // let layerArr
    if (item.type === 'TileLayer') {
      const layerArr = getTileLayers(item.source)
      layerArrs = [...layerArrs, ...layerArr]
    }
  }
  return layerArrs
}
